"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.onDestroy = exports.onPublish = void 0;
const GenCSharp = require("./GenCSharp");
const csharp_1 = require("csharp");
const App = csharp_1.FairyEditor.App;
App.pluginManager.LoadUIPackage(App.pluginManager.basePath + "/" + eval("__dirname") + '/LabelTranslate');
class DemoInspector extends csharp_1.FairyEditor.View.PluginInspector {
    textinput;
    btn_switch;
    show_obj;
    obj_type;
    constructor() {
        super();
        this.panel = csharp_1.FairyGUI.UIPackage.CreateObject("LabelTranslate", "Main").asCom;
        this.textinput = this.panel.GetChild("input").asLabel;
        this.btn_switch = this.panel.GetChild("switch").asButton;

        this.btn_switch.onClick.Set(() => {
            this.show_obj.docElement.SetProperty("customData", JSON.stringify({ "translate": this.btn_switch.selected, "key": this.textinput.text, "type": this.obj_type }));
            let name = App.activeDoc.inspectingTarget.name;
            if (name[0] == "n" && /^\d+$/.test(name.substring(1))) {
                App.activeDoc.inspectingTarget.name = "tr_" + name.substring(1);
            }
        });

        this.textinput.onKeyDown.Set(() => {
            this.show_obj.docElement.SetProperty("customData", JSON.stringify({ "translate": this.btn_switch.selected, "key": this.textinput.text, "type": this.obj_type }));
            let name = App.activeDoc.inspectingTarget.name;
            if (name[0] == "n" && /^\d+$/.test(name.substring(1))) {
                App.activeDoc.inspectingTarget.name = "tr" + name.substring(1);
            }
        })

        this.updateAction = () => { return this.updateUI(); };
    }
    updateUI() {
        let obj = App.activeDoc.inspectingTarget;
        let isShow = false;
        switch (obj.objectType) {
            case csharp_1.FairyEditor.FObjectType.TEXT:
                isShow = true;
                break;
            case csharp_1.FairyEditor.FObjectType.COMPONENT:
                if (!!obj.text) {
                    isShow = true;
                    this.obj_type = 0;
                } else {
                    let count = obj.Write().Elements().Count;
                    for (let i = 0; i < count; i++) {
                        if (obj.Write().Elements().get_Item(i).GetAttribute("target") == "Text") {
                            isShow = true;
                            this.obj_type = 1;
                            break;
                        }
                    }
                }
                break;
        }

        if (isShow) {
            let data = obj.customData;
            if (data) {
                let j_data = JSON.parse(data);
                this.btn_switch.selected = j_data.translate ? true : false;
                this.textinput.text = j_data.key;
            } else {
                this.btn_switch.selected = false;
                this.textinput.text = "";
            }
        }
        this.show_obj = obj;
        return isShow;
    }
}

//Register a inspector
App.inspectorView.AddInspector(() => new DemoInspector(), "LabelTranslate", "多语言");
//Condition to show it
App.docFactory.ConnectInspector("LabelTranslate", "mixed", false, false);

exports.onPublish = (handler) => {
    if (!handler.genCode)
        return;
    handler.genCode = false;
    GenCSharp.genCode(handler);
};